using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Microsoft.Services.TfsAlert.Core.BusinessFacades;
using Microsoft.Services.TfsAlert.Core.BusinessFacades.TeamFoundationBF;
using Microsoft.Services.TfsAlert.Core.Model;

namespace Microsoft.Services.TfsAlert.UnitTests.Core
{
    [TestClass]
    public class TeamFoundationFacadeTest
    {
        [TestMethod]
        public void CanCreateBusinessFacadeFactoryIntance()
        {
            IBusinessFacadeFactory _businessFacadeFactory = BusinessFacadeFactory.CreateInstance();

            Assert.IsNotNull(_businessFacadeFactory);
          
        }

        [TestMethod]
        public void CanCreateTeamFoundationFacadeInstance()
        {
            IBusinessFacadeFactory _businessFacadeFactory = BusinessFacadeFactory.CreateInstance();
            ITeamFoundationFacade _tfFacade = _businessFacadeFactory.CreateTeamFoundationBF();

            Assert.IsNotNull(_tfFacade);
        }

        [TestMethod]
        public void CanRetrieveAllServersFromFacade()
        {
            IBusinessFacadeFactory _businessFacadeFactory = BusinessFacadeFactory.CreateInstance();
            ITeamFoundationFacade _tfFacade = _businessFacadeFactory.CreateTeamFoundationBF();
            List<TfsServer> _servers = _tfFacade.RetrieveAllServers();
            
            Assert.IsNotNull(_servers);
          
        }
        
        [TestMethod]
        public void CanCallUpdateAllServersUsingFacade()
        {
            IBusinessFacadeFactory _businessFacadeFactory = BusinessFacadeFactory.CreateInstance();
            ITeamFoundationFacade _tfFacade = _businessFacadeFactory.CreateTeamFoundationBF();

            List<TfsServer> _servers = CreateServers();
            
            _tfFacade.Update(_servers);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void ExceptionInvalidArguement()
        {
            IBusinessFacadeFactory _businessFacadeFactory = BusinessFacadeFactory.CreateInstance();
            ITeamFoundationFacade _theFacade = _businessFacadeFactory.CreateTeamFoundationBF();
            TfsServer _theServer =  _theFacade.RetrieveServer("");
        }

        


        private List<TfsServer> CreateServers()
        {
            List<TfsServer> _servers = new List<TfsServer>();
            TfsServer _server1 = new TfsServer();
            _server1.Name = "TestServer1";
            _server1.URI ="Http://testserver1";
            _server1.UserName = "UserName1";

            TfsServer _server2 = new TfsServer();
            _server2.Name = "TestServer2";
            _server2.URI = "Http://testserver2";
            _server2.UserName = "UserName2";

            TfsEvent _event = new TfsEvent();
            _event.EventType = "CheckIn";
            _event.FilterExpression = String.Empty;
            _event.IsSelected = false;
            _event.SubscriptionID = 0;

            TfsEvent _event1 = new TfsEvent();
            _event.EventType = "CheckOut";
            _event.FilterExpression = "'Artifacts/Artifact[starts-with(@ServerItem, \"$/RangerCI/RangerCISolution\")]' <> null";
            _event.IsSelected = false;
            _event.SubscriptionID = 0;

            List<TfsEvent> _events = new List<TfsEvent>();
            _events.Add(_event);
            _events.Add(_event);

            _server1.Events = _events;
            _server2.Events = _events;

            _servers.Add(_server1);
            _servers.Add(_server2);

            return _servers;
        }
    }
}
